package com.bjsdzk.collector.timer;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bjsdzk.collector.entity.RealTimeData;

@Component
@Profile("dazhong")
public class DazhongTask {

	private static Logger logger = LoggerFactory.getLogger(DazhongTask.class);
	
	@Autowired
    private MongoTemplate mongoTemplate;
	
	@Autowired
    private MongoOperations mongoOperations;

	    
	@PostConstruct
	public void init() {
		

	}
	
	@Scheduled(cron = "0 0 1 * * ?")
	public void accessToken() {
		Calendar occurTime = Calendar.getInstance();
		occurTime.add(Calendar.YEAR, -3);
		int beforeYear = occurTime.get(Calendar.YEAR);
		
		Set<String> collectionNames = mongoTemplate.getCollectionNames();
		collectionNames.forEach((tableName) -> {
			if(tableName.matches("SDZK_\\d{10}")) {
				int year=Integer.parseInt(tableName.substring(5,9));
				if(beforeYear>year) {
					mongoTemplate.dropCollection(tableName);
					logger.info("移除三年前的数据...tableName:"+tableName);
				}
			}
		});
		
	}
}
